package com.example.module_home.Utils;

import android.databinding.BindingAdapter;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ListView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import br.tiagohm.markdownview.MarkdownView;
import br.tiagohm.markdownview.css.InternalStyleSheet;
import br.tiagohm.markdownview.css.styles.Github;

/**
 * Created by 陈煜 on 2018/5/4.
 */

public class DBUtils {
    private static final String TAG = "DBUtils";
//    @BindingAdapter({"webContent"})
//    public static void setWebContent(MarkdownView view,String code){
//        Log.e(TAG, "setWebContent: code = "+code );
//        InternalStyleSheet style = new Github();
//        ((Github) style)
//                .addCodeRule("pre,xmp,plaintext,listing","display: block")
//                .addCodeRule(".token.keyword","color: #39c")
//                .addCodeRule(".token.function","color: #eeaa00")
//                .addCodeRule(".token.punctuation","color: #a67f59")
//                .addCodeRule(".token.operator","color: #eeaa00")
//                .addCodeRule(".token.comment","color: #43a943")
//                .addCodeRule(".token.string","color: #ffffff")
//                .addCodeRule(".token.number","color: #a67f59")
//                .addCodeRule(".token.class-name","color: white")
//                .addCodeRule(".token.tag","color: #a67f59")
//                .addCodeRule(".token.namespace","color: #ffffff")
//                .addCodeRule(".token.attr-name","color: #ffffff")
//                .addCodeRule(".token.attr-value","color: #39c")
//                .addCodeRule(".token.prolog","color: #43a943")
//                .addCodeRule("pre[class*=\"language-\"],code[class*=\"language-\"]","background: #202020")
//                .addCodeRule("pre[class*=\"language-\"]","padding: 1em","margin: .5em 0","overflow: auto")
//                .addCodeRule("pre[class*=\"language-\"],code[class*=\"language-\"]","color: white","text-shadow: none","font-family: Consolas, Monaco, \'Andale Mono\', \'Ubuntu Mono\', monospace",
//                        "direction: ltr","text-align: left","white-space: pre","word-spacing: normal","word-break: normal","word-wrap: normal","line-height: 1.5","tab-size: 4","hyphens: none");
//        view.addStyleSheet(style);
//        if(code!=null){
//            view.loadMarkdown(replaceSpace(code));
//        }
//    }

    @BindingAdapter({"webContent"})
    @RequiresApi(api= Build.VERSION_CODES.KITKAT)
    public static void setWebContent(final WebView webContent, final String code){
        String mPath = "file:///android_asset/tinyMce/edit.html";
        webContent.loadUrl(mPath);
        if(code!=null){
            webContent.setWebViewClient(new WebViewClient(){
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    String webCode = null;
                    try {
                        webCode = URLEncoder.encode(code,"UTF-8");
                    } catch (UnsupportedEncodingException IE) {
                        IE.printStackTrace();
                    }
                    webContent.evaluateJavascript("javascript:setHtml('"+webCode+"')", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            //此处为 js 返回的结果
                        }
                    });

                }
            });
        }
    }



    private static String replaceSpace(String str) {
        char[] cs = str.toCharArray();
        int brace = 0;
        boolean quota = false;
        for (int i = 0; i < cs.length; i++) {
            switch (cs[i]) {
                case '>':
                    brace++;
                    break;
                case '<':
                    brace--;
                    break;
                case '"':
                    quota = !quota;
                    break;
                case 32: // ASCII of Space
                    if (!quota && brace==0) cs[i] = '\b';
            }
        }
        return String.valueOf(cs);
    }
}
